package com.xuxueli.applyModules.commont.testEntityFeids;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;
import java.math.BigDecimal;

public class XdoAgent {

    public static void Install() {
        premain(null, ByteBuddyAgent.install());
    }

    private static void premain(String arguments, Instrumentation instrumentation) {
        new AgentBuilder.Default()
                .type(ElementMatchers.isAnnotatedWith(RemoveTailingZero.class))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
                        return builder.method(ElementMatchers.returns(BigDecimal.class).
                                and(ElementMatchers.isGetter())).intercept(MethodDelegation.to(BigDecimalInterceptor.class));
                    }
                }).installOn(instrumentation);
    }

}
